package com.cn.springcloudwechat.serviceImpl.courseall;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.EmptyUtil;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.mapper.mapper.attachment.YmAttachmentMapper;
import com.cn.springcloudwechat.mapper.mapper.attachment.YmAttachmentRelevanceMapper;
import com.cn.springcloudwechat.mapper.mapper.find.courseall.YmCourseCoursePackageMapper;
import com.cn.springcloudwechat.mapper.mapper.find.courseall.YmCourseMapper;
import com.cn.springcloudwechat.mapper.mapper.find.courseall.YmCoursePackageMapper;
import com.cn.springcloudwechat.model.YmCourse;
import com.cn.springcloudwechat.model.YmCoursePackage;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.attachment.YmAttachment;
import com.cn.springcloudwechat.model.attachment.YmAttachmentRelevance;
import com.cn.springcloudwechat.model.cos.CosClientEntity;
import com.cn.springcloudwechat.model.YmCourseCoursePackage;
import com.cn.springcloudwechat.service.courseall.CoursePackageService;
import com.cn.springcloudwechat.upload.CosUtils;
@Service("CoursePackageService")
public class CoursePackageServiceImpl implements CoursePackageService {
	@Autowired
	YmCoursePackageMapper coursePackageMapper;
	@Autowired
	YmAttachmentMapper ymAttachmentMapper;
	@Autowired
	YmAttachmentRelevanceMapper ymAttachmentRelevanceMapper;
	@Autowired
	YmCourseMapper ymCourseMapper;
	@Autowired
	YmCourseCoursePackageMapper ymCourseCoursePackageMapper;
	
	/**
	 * 课程包的发布
	 * 
	 * @param YmCoursePackage ymcoursePackage,String[] parameter
	 *           
	 * @return SUCC
	 */
	@Override
	@Transactional
	public Long insertCoursePackageSer(YmCoursePackage ymcoursePackage,String[] parameter){
		try {
			Assert.state(!EmptyUtil.objFieldsIsEmpty(ymcoursePackage),String.format(ErrorUtil.PARAMETER_NULL,"coursePackage"));
	//		String path = CosUtils.SimpleUploadFileFromLocal(ymcoursePackage.getPath());
			ymcoursePackage.setCollectionNumber(0l);
			ymcoursePackage.setSaleTotalNumber(0);
			ymcoursePackage.setState((short)1);
			coursePackageMapper.insertSelective(ymcoursePackage);
			YmCourseCoursePackage YmCourseCoursePackage = new YmCourseCoursePackage();
			Integer orderNumber = 1;
			//课程包关联课程
			for(String pt : parameter){
			YmCourseCoursePackage.setCoursePackageId(ymcoursePackage.getCoursePackageId());
			YmCourseCoursePackage.setCourseId(Long.parseLong(pt));
			YmCourseCoursePackage.setCourseOrder(orderNumber);
			YmCourseCoursePackage.setState((short)1);
			ymCourseCoursePackageMapper.insertSelective(YmCourseCoursePackage);
			orderNumber ++;
			}
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(String.format(ErrorUtil.COURSE_ERROR));
		}
		return ymcoursePackage.getCoursePackageId();
	}

	/**
	 * 课程包的修改(包含删除的有效和无效)
	 *
	 * @return SUCC
	 */
	@Override
	@Transactional
	public void updateCoursePackageSer(YmCoursePackage ymcoursePackage,String[] parameter){
		try {
			Assert.state(!EmptyUtil.objFieldsIsEmpty(ymcoursePackage),String.format(ErrorUtil.PARAMETER_NULL,"coursePackage"));
			Assert.notNull(ymcoursePackage.getCoursePackageId(),String.format(ErrorUtil.PARAMETER_NULL, "主键"));
		//	String path = CosUtils.SimpleUploadFileFromLocal(ymcoursePackage.getPath());
			coursePackageMapper.updateByPrimaryKeySelective(ymcoursePackage);
			if(ymcoursePackage.getPath()!=null && !"".equals(ymcoursePackage.getPath())){
			File file = new File(ymcoursePackage.getPath());
			String suffix=file.getName().substring(file.getName().lastIndexOf(".")+1);      
			YmAttachment YmAttachment = new YmAttachment();
			YmAttachment.setAttachmentName(file.getName());
			YmAttachment.setAttachmentSuffix(suffix);
			YmAttachment.setAttachmentUrl(ymcoursePackage.getPath());
			YmAttachment.setAttachmentType("3");
			ymAttachmentMapper.insertSelective(YmAttachment);
			YmAttachmentRelevance ymAttachmentRelevance = new YmAttachmentRelevance();
			ymAttachmentRelevance.setRelevanceId(ymcoursePackage.getCoursePackageId());
			ymAttachmentRelevance.setAttachmentId(YmAttachment.getAttachmentId());
			ymAttachmentRelevance.setRelevanceTpyeId("6");
			ymAttachmentRelevance.setState((short)1);
			ymAttachmentRelevanceMapper.updateSelective(ymAttachmentRelevance);
			}
			if(parameter.length>0){
			ymCourseCoursePackageMapper.deleteByPrimaryKey(ymcoursePackage.getCoursePackageId());
			}
			YmCourseCoursePackage YmCourseCoursePackage = new YmCourseCoursePackage();
			Integer orderNumber = 1;
			//课程包关联课程
			for(String pt : parameter){
			YmCourseCoursePackage.setCoursePackageId(ymcoursePackage.getCoursePackageId());
			YmCourseCoursePackage.setCourseId(Long.parseLong(pt));
			YmCourseCoursePackage.setCourseOrder(orderNumber);
			YmCourseCoursePackage.setState((short)1);
			ymCourseCoursePackageMapper.insertSelective(YmCourseCoursePackage);
			orderNumber ++;
			}
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(String.format(ErrorUtil.COURSE_ERROR));
		}
	}

	/**
	 * 课程发布
	 * 
	 * @param Course course
	 *           
	 * @return SUCC
	 */
	@Override
	@Transactional
	public Long insertCourseSer(YmCourse ymCourse){
		try {
			Assert.state(!EmptyUtil.objFieldsIsEmpty(ymCourse),String.format(ErrorUtil.PARAMETER_NULL,"course"));
		   /* String videoPath = CosUtils.SimpleUploadFileFromLocal(ymCourse.getPath());
		    String imgPath = CosUtils.SimpleUploadFileFromLocal(ymCourse.getImgPath());*/
			ymCourse.setState((short)1);
			ymCourseMapper.insertSelective(ymCourse);
			YmAttachment YmAttachment = new YmAttachment();
			YmAttachmentRelevance ymAttachmentRelevance = new YmAttachmentRelevance();
   	    	if(ymCourse.getPath()!=null && !"".equals(ymCourse.getPath())){
			File file = new File(ymCourse.getPath());
			String suffix=file.getName().substring(file.getName().lastIndexOf(".")+1);      
			YmAttachment.setAttachmentName(file.getName());
			YmAttachment.setAttachmentSuffix(suffix);
                YmAttachment.setAttachmentUrl(ymCourse.getPath());
			YmAttachment.setAttachmentType("3");
			ymAttachmentMapper.insertSelective(YmAttachment);
			ymAttachmentRelevance.setRelevanceId(ymCourse.getCourseId());
			ymAttachmentRelevance.setAttachmentId(YmAttachment.getAttachmentId());
			ymAttachmentRelevance.setRelevanceTpyeDetail(3);
			ymAttachmentRelevance.setRelevanceTpyeId("5");
			ymAttachmentRelevance.setState((short)1);
			ymAttachmentRelevanceMapper.insertSelective(ymAttachmentRelevance);
   	    	}	
   	    	if(ymCourse.getImgPath()!=null && !"".equals(ymCourse.getImgPath())){
			File file1 = new File(ymCourse.getImgPath());
			String suffix1=file1.getName().substring(file1.getName().lastIndexOf(".")+1);      
			YmAttachment.setAttachmentName(file1.getName());
			YmAttachment.setAttachmentSuffix(suffix1);
			YmAttachment.setAttachmentUrl(ymCourse.getImgPath());
			YmAttachment.setAttachmentType("3");
			ymAttachmentMapper.insertSelective(YmAttachment);
			ymAttachmentRelevance.setRelevanceId(ymCourse.getCourseId());
			ymAttachmentRelevance.setAttachmentId(YmAttachment.getAttachmentId());
			ymAttachmentRelevance.setRelevanceTpyeDetail(1);
			ymAttachmentRelevance.setRelevanceTpyeId("5");
			ymAttachmentRelevance.setState((short)1);
			ymAttachmentRelevanceMapper.insertSelective(ymAttachmentRelevance);
   	    	}
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(String.format(ErrorUtil.COURSE_ERROR));
		}
    return ymCourse.getCourseId();
	}
	
	/**
	 * 课程的修改(状态有效无效)
	 * 
	 * @return SUCC
	 */
	@Override
	@Transactional
	public void updateCourseSer(YmCourse ymCourse){
		try {
			Assert.state(!EmptyUtil.objFieldsIsEmpty(ymCourse),String.format(ErrorUtil.PARAMETER_NULL,"ymCourse"));
		//	String videoPath = CosUtils.SimpleUploadFileFromLocal(ymCourse.getPath());
			ymCourseMapper.updateByPrimaryKeySelective(ymCourse);
			YmAttachment YmAttachment = new YmAttachment();
			YmAttachmentRelevance ymAttachmentRelevance = new YmAttachmentRelevance();
			if(ymCourse.getPath()!=null && !"".equals(ymCourse.getPath())){
			File file = new File(ymCourse.getPath());
			String suffix=file.getName().substring(file.getName().lastIndexOf(".")+1);      
			YmAttachment.setAttachmentName(file.getName());
			YmAttachment.setAttachmentSuffix(suffix);
			YmAttachment.setAttachmentUrl(ymCourse.getPath());
		//	YmAttachment.setAttachmentUrl(CosClientEntity.realmName+"/"+videoPath);
			YmAttachment.setAttachmentType("3");
			ymAttachmentMapper.insertSelective(YmAttachment);
			ymAttachmentRelevance.setRelevanceId(ymCourse.getCourseId());
			ymAttachmentRelevance.setAttachmentId(YmAttachment.getAttachmentId());
			ymAttachmentRelevance.setRelevanceTpyeId("5");
			ymAttachmentRelevance.setRelevanceTpyeDetail(3);
			ymAttachmentRelevance.setState((short)1);
			ymAttachmentRelevanceMapper.updateSelective(ymAttachmentRelevance);
			}
			if(ymCourse.getImgPath()!=null && !"".equals(ymCourse.getImgPath())){
			File file1 = new File(ymCourse.getImgPath());
			String suffix1=file1.getName().substring(file1.getName().lastIndexOf(".")+1);      
			YmAttachment.setAttachmentName(file1.getName());
			YmAttachment.setAttachmentSuffix(suffix1);
            YmAttachment.setAttachmentUrl(ymCourse.getImgPath());
			YmAttachment.setAttachmentType("3");
			ymAttachmentMapper.insertSelective(YmAttachment);
			ymAttachmentRelevance.setRelevanceId(ymCourse.getCourseId());
			ymAttachmentRelevance.setAttachmentId(YmAttachment.getAttachmentId());
			ymAttachmentRelevance.setRelevanceTpyeDetail(1);
			ymAttachmentRelevance.setRelevanceTpyeId("5");
			ymAttachmentRelevance.setState((short)1);
			ymAttachmentRelevanceMapper.updateSelective(ymAttachmentRelevance);
   	    	}
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(String.format(ErrorUtil.COURSE_ERROR));
		}
	}

	/**
	 * 课程包的查询
	 * 
	 * @param YmCoursePackage ymcoursePackage
	 *           
	 * @return List<YmCoursePackage>
	 */
	@Override
	public Page<Object> queryCoursePackageSer(YmCoursePackage ymcoursePackage,Page<Object> page){
		    List<Object> Objects = new ArrayList<Object>();
		    Map<String,Object> map = new HashMap<String, Object>();
		    List<YmCoursePackage> YmCoursePackages = coursePackageMapper.selectByPrimaryKey(ymcoursePackage,page);
		    if(ymcoursePackage.getCoursePackageId()!=null && !"".equals(ymcoursePackage.getCoursePackageId())){
		    if(ymcoursePackage.getType() == null) {
		    for(int i = 0;i < YmCoursePackages.size();i++){
		    YmUser ymUser = (YmUser) ThreadLocalUtil.getUser();
	    	map.put("goodsId",ymcoursePackage.getCoursePackageId());
	    	map.put("userId",ymUser.getUserId());
	    	Integer isBuy = coursePackageMapper.IsBuy(map);
	    	YmCoursePackages.get(i).setIsBuy(isBuy);
		      }
		    }
		    }
		    Objects.addAll(YmCoursePackages);
		    page.setResults(/*coursePackageMapper.selectByPrimaryKey(ymcoursePackage,page)*/Objects);
		    return page;
	}

	/**
	 * 课程包的查询(明细)
	 * 
	 * @param Long coursePackageId
	 *           
	 * @return List<YmCoursePackage>
	 */
	@Override
	public List<YmCoursePackage> queryCoursePackageDetailSer(Long coursePackageId) {
		   Assert.notNull(coursePackageId,String.format(ErrorUtil.PARAMETER_NULL, "主键"));
		   return coursePackageMapper.queryCoursePackageDetailMapper(coursePackageId);
	}
	
	/**
	 * 课程的查询
	 * 
	 * @param YmCourse ymcourse
	 *           
	 * @return List<YmCourse>
	 */

	public Page<Object> queryCourseSer(YmCourse ymCourse,Page<Object> page) {
		  page.setResults(ymCourseMapper.selectByPrimaryKey(ymCourse,page));
		  return page;
	}

	/**
	 * 课程包的收藏
	 * 
	 * @param Long coursePackageId
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateCollectionNumberSer(Long coursePackageId) {
		  Assert.notNull(coursePackageId,String.format(ErrorUtil.PARAMETER_NULL, "主键"));
		  coursePackageMapper.updateCollectionNumberMapper(coursePackageId);
	}

	@Override
	public void updateCourseStateSer(String[] parameter, Integer state) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("list",parameter);
		map.put("state",state);
		ymCourseMapper.updateCourseStateMapper(map);
	}

	@Override
	public void updateCoursePackageStateSer(Integer state,String[] parameter) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("list",parameter);
		map.put("state",state);
		coursePackageMapper.updateCoursePackageStateMapper(map);
	}
}
